#将局部变量变成全局变量，用global关键字
a = 1
def foo():
    global a
    a = 2
    print(a)
foo()
print(a)


#将局部变量变成自由变量，用nonlocal关键字
def make_averager():
    total = 0
    count  = 0
    def averager(value):
        nonlocal total, count
        total += value
        count +=1
        return total / count
    return averager

my_avg = make_averager()
print(my_avg(1))
print(my_avg(2))
print(my_avg(3))
print(my_avg(4))


#局部变量定义优先
a = 1
b = []
def foo():
    a = 2
    b.append(2)
    #b = 3 先执行定义的b = 3#
    return None
foo()
print(a)
print(b)




